import scrapy
from scrapy import Request
import pandas as pd

from Project.DogRecruitment.DogRecruitment.items import FindPeopleDetailItem


class Dog_Recruitment(scrapy.Spider):
    name = "FindPeopleDetail"
    data = pd.read_csv(r"D:\code\Python-Project\Scrapy\Project\DogRecruitment\FindPeople.csv",encoding='utf-8')
    resumeId = data['resumeId'].values
    start_urls = [f"https://www.5iai.com/api/resume/baseInfo/public/{i}" for i in resumeId]


    def start_requests(self):
        for url in self.start_urls:
            yield Request(url, dont_filter=True,callback=self.parse)

    def parse(self, response, **kwargs):
        r = response.json()['data']
        item = FindPeopleDetailItem()

        item['resumeId'] = response.url.split("/")[-1]
        item['resumeName'] = r['resumeName']
        item['birthday'] = r['birthday']
        item['address'] = r['address']
        item['arrivalTime'] = r['arrivalTime']
        item['politicalStatus'] = r['politicalStatus']
        item['selfEvaluation'] = r['selfEvaluation']
        item['expectIndustry'] = r['expectIndustry']
        item['willNature'] = r['willNature']
        # 列表类型
        temp_list = []
        for i in r['keywordList']:
            temp_list.append(i['labelName'])
        item['keywordList'] = temp_list

        temp_list2 = []
        for i in r['educationExperienceList']:
            temp_list2.append(str(i['school'])+"|"+str(i['speciality'])+"|"+str(i['educationalBackground'])+"|"+str(i['studyTime'])+"|"+str(i['graduationTime']))
        item['educationExperienceList'] = temp_list2

        temp_list3 = []
        for i in r['projectExperienceList']:
            temp_list3.append(str(i["projectName"])+"|"+\
            str(i["company"])+"|"+\
            str(i["roleName"])+"|"+\
            str(i["startTime"])+"|"+\
            str(i["endTime"])+"|"+\
            str(i["description"])+"|"+\
            str(i["achievement"]))
        item['projectExperienceList'] = temp_list3

        temp_list4 = []
        for i in r['competitionExperienceList']:
            temp_list4.append(str(i["type"]) + "|" + \
            str(i["name"]) + "|" + \
            str(i["level"]) + "|" + \
            str(i["certificate"]))
        item['competitionExperienceList'] = temp_list4

        temp_list5 = []
        for i in r['trainingExperienceList']:
            temp_list5.append(str(i["orgName"]) + "|" + \
            str(i["recordName"]) + "|" + \
            str(i["startTime"]) + "|" + \
            str(i["endTime"]) + "|" + \
            str(i["description"]))
        item['trainingExperienceList'] = temp_list5

        # temp_list6 = []
        # for i in r['professionalList']:
        #     temp_list6.append(
        #         i['id']
        #     )
        # item['professionalList'] = temp_list6


        temp_list7 = []
        for i in r['skillList']:
            temp_list7.append(str(i["name"]) + "|" + \
            str(i["level"]))
        item['skillList'] = temp_list7


        temp_list8 = []
        for i in r['languageList']:
            temp_list8.append(str(i["name"]) + "|" + \
            str(i["level"]))
        item['languageList'] = temp_list8


        temp_list9 = []
        for i in r['certList']:
            temp_list9.append(str(i["type"]) + "|" + \
            str(i["name"]) + "|" + \
            str(i["getTime"]) + "|" + \
            str(i["attachment"]))
        item['certList'] = temp_list9


        temp_list10 = []
        for i in r['workExperienceList']:
            temp_list10.append(str(i["willNature"]) + "|" + \
            str(i["company"]) + "|" + \
            str(i["industry"]) + "|" + \
            str(i["positionName"]) + "|" + \
            str(i[ "joinTime"]) + "|" + \
            str(i[ "outTime"]) + "|" + \
            str(i["description"]))
        item['workExperienceList'] = temp_list10


        temp_list11 = []
        for i in r['attachmentList']:
            temp_list11.append(
            str(i["name"]) + "|" + \
            str(i["description"]) + "|" + \
            str(i["file"]))
        item['attachmentList'] = temp_list11

        yield item
